#include "select.hpp"

SelectScreen::SelectScreen()
{
	clrstages = 0xFC;
}

SelectScreen::~SelectScreen()
{
}

#include "image.hpp"
#include "sound.hpp"

static const int STAGECT = 4;
static Image *stgimgs[STAGECT];

enum
{
	CURSOR,
	BUZZ,
	SNDCT
};
static Sound *snds[SNDCT];

#include "camera.hpp"
#include "video.hpp"
#include "music.hpp"

bool SelectScreen::Open()
{
	const char *stgstrs[STAGECT] =
	{
		"stg1",
		"stg2",
		"stg3",
		"stg4"
	};

	const char *sndstrs[SNDCT] =
	{
		"cursor",
		"buzz",
	};

	AllImgs.BatchGet(stgimgs, STAGECT, "select/", stgstrs);

	AllSnds.BatchGet(snds, SNDCT, "menu/", sndstrs);

	Cam.Min(0, 0);
	Cam.Max(Cam.size.x, Cam.size.y);
	Cam.Pos(0, 0);
	Cam.Out(0);
	
	Vid.Flip(false, false);

	//Mus.Play(Mus.INTER);
	return true;
}

void SelectScreen::Close()
{
	AllImgs.Clear();
}

#include "audio.hpp"
#include "input.hpp"
#include "title.hpp"
#include "app.hpp"
#include "game.hpp"

void SelectScreen::In()
{
	if (Inp.CmdEdge(Inp.EXIT))
	{
		App.ScreenChange(&Title);
		return;
	}

	int OKCMDS = Inp.CmdBit(Inp.SHOOT) | Inp.CmdBit(Inp.ENTER);
	if (Inp.EdgeBits() & OKCMDS)
	{
		if ((1 << cursor) & clrstages)
		{
			Aud.SoundPlay(*snds[BUZZ]);
		}
		else
		{
			Mus.Stop();
			Mus.Close();
			Game.StageReset(cursor + 1);
			//Game.StageSet(cursor + 1);
			App.ScreenChange(&Game);

			Aud.SoundPlay(*snds[CURSOR]);
		}
	}
	else 
	for (int c = 0; c < Inp.SHOOT; ++c)
	{
		if (Inp.CmdEdge(c))
		{
			cursor = c;
			Aud.SoundPlay(*snds[CURSOR]);
		}
	}
}

void SelectScreen::Update()
{
}


void SelectScreen::Out(float upfrac)
{
	const Vec2f STAGEPOS[STAGECT] =
	{
		Vec2f(2, 15),
		Vec2f(30, 11),
		Vec2f(12, 2),
		Vec2f(20, 24)
	};

	Vid.Pri(0);

	for (int i = 0; i < STAGECT; ++i)
	{
		Image *img = stgimgs[i];
		if (!img)
			continue;

		float sat = 1;

		if ((1 << i) & clrstages)
		{
			sat *= .5f;
		}

		if (cursor != i)
		{
			sat *= .5f;
		}

		Vid.Color(sat, sat, sat, 1);
		Vid.Pos(STAGEPOS[i].x, STAGEPOS[i].y);
		Vid.SpriteDraw(*img);
	}

	Vid.Color(1, 1, 1, 1);
}

SelectScreen Select;
